﻿using DocumentFormat.OpenXml.Office2010.ExcelAc;
using NPOI.SS.Util;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using TradeDataAPI.Core.Configuration;
using TradeDataAPI.Entity.DomainModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TradeDataAPI.Core.Common
{
    public class NumberHelper
    {

        public static int GetNumberByStr(string Str)
        {
            if (Str=="待处理")
            {
                return 2;
            }
            else if (Str == "无效")
            {
                return 3;
            }
            else if(Str.Contains("成功") || Str.Contains("完成"))
            {
                return 1;
            }

            return 0;
        }

 

        /// <summary>
        /// 判断角色是否存在于配置文件中
        /// </summary>
        /// <param name="Roles"></param>
        /// <returns></returns>
        public static bool CheckRoles(string Roles)
        {
            var RolesList = AppSetting.GetSection("TradeDataAPI")["ViewRoles"];
            if (RolesList.Contains(Roles))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 判断字符串中是否包含指定字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckExits(string str)
        {
            var SuccessMsg = AppSetting.GetSection("TradeDataAPI")["SuccessMsg"];
            return SuccessMsg.Any(item => str.Contains(item));
        }

        /// <summary>
        /// 生成一个6位数的随机数
        /// </summary>
        /// <returns></returns>
        public static string GenerateNumber()
        {
            Random random = new Random();
            int randomNumber = random.Next(10000, 100000); // 生成一个4位数
            return "1" + randomNumber.ToString("D5"); // 确保总长度为6位
        }



        /// <summary>
        /// 提报参与率
        /// </summary>
        /// <param name="effective">有效隐患的提报人数</param>
        /// <param name="total">部门总人数</param>
        /// <returns>提报参与率</returns>
        public static float ParticipationRate(int effective, int total)
        {
            if (total == 0)
            {
                return 0;
            }
            float participationRate = ((float)effective / total) * 100;
            participationRate = (float)Math.Round(participationRate, 2);
            return  participationRate;
        }


        /// <summary>
        /// 改善率
        /// </summary>
        /// <param name="effective">有效改善条数</param>
        /// <param name="total">部门总人数</param>
        /// <returns></returns>
        public static async Task<float> ImprovementRate(int effective, int total)
        {
            if (total == 0)
            {
                return 0;
            }
            float participationRate = ((float)effective / total) * 100;
            participationRate = (float)Math.Round(participationRate, 2);
            return participationRate;
        }
    }
}
